<?php
namespace app\admin\controller;
use app\admin\controller\AdminController;
/**
 * 后台用户
 */
class AdminUpFilesController extends AdminController {

    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '上传图片清理',
                'description' => '清理未被使用的上传图片，减少空间占用',
                ),
            'menu' => array(
                    array(
                        'name' => '上传记录',
                        'url' => url('index'),
                        'icon' => 'list',
                    ),
                ),
            'add' => array(
                    array(
                        'name' => '开始检查',
                        'url' => url('check'),
                    ),
                ),
            );
    }
    /**
     * 清理日志
     */
    public function index(){
        $where = array();
        $limit = 50;
        $list = target('File')->page(20)->loadList($where,$limit);
        $this->pager = target('File')->pager;
        //位置导航
        $breadCrumb = array('上传记录'=>url());
        //模板传值
        $this->assign('breadCrumb',$breadCrumb);
        $this->assign('list',$list);
        $this->assign('page',$this->getPageShow($pageMaps));
        $this->adminDisplay();
    }

    /**
     * 检查
     */
    public function check(){

        $Fileslist = array();$Tables = array(); $Fields = array(); $DelFileList = array(); $FindResult = array();
        $list = target('File')->loadList(array(),array(),"file_id,url");
        foreach ($list as $key => $val){
            $Fileslist[$val['file_id']] = $val['url'];
        }
        $Fileslist = array_unique($Fileslist);
        $No_check_table = array(
            'admin_group','admin_log','admin_user','category_article','config','config_upload',
            'field','field_expand','field_form','fieldset','fieldset_expand','fieldset_form','position','tags','tags_has',
            'total_spider','total_visitor','file'
        );
        //获取所有表
        $list = target('Database')->loadTableList();
        foreach ($list as $key => $val){
            //过滤掉指定的表
            if(!in_array(str_replace("dux_", "", $val['Name']),$No_check_table))$Tables[] = $val['Name'];
        }
        
        foreach ($Tables as $key => $val) {
            $Tables[$key]['_fields'] = array();
            //提取当前表所有字段名
            $tmp_fields = target('Database')->loadFieldList($val);
            $arr_fields = array();
            foreach ($tmp_fields as $k=>$v){
                $tmp_type = substr($v['Type'],0,4);
                //对字段进行过滤，只提取文本类型字段
                if($tmp_type == "text" || $tmp_type == "varc" || $tmp_type == "medi"){
                    $arr_fields[] = $v['Field'];
                }
            }
            $Tables[$key] = array(
              'table_name' => str_replace("dux_", "", $val),
              '_fields' => $arr_fields  
            );
        }
        //搜索所有数据表中的所有列出字段内容中，是否包括图片
        $where = array();
        foreach ($Tables as $key => $val){
            foreach ($val['_fields'] as $k => $v){
                $tmp_where[] = '('.$v.' like "%.jpg%") OR ('.$v.' like "%.png%") OR ('.$v.' like "%.bmp%") OR ('.$v.' like "%.gif%")';
            }
            $tmp2_where = implode(" OR ", $tmp_where);
            $where[] = $tmp2_where;
            $field = implode(",", $val['_fields']);
            $FindResult[] = target("File")->PublicQuery($val['table_name'],$where,$field);
            $tmp_where = "";
            $tmp2_where = "";
            unset($where);
        }
        $AllImages = array();
        foreach ($FindResult as $key => $value) {
            if(!is_null($value)){
                $AllImages = array_merge($AllImages,$value);
            }
        }
        $AllImages = array_unique($AllImages);

        $result = array_diff($Fileslist,$AllImages);
        $this->assign('list',$result);
        $this->adminDisplay('check');
    }

    /**
     * 清理
     */
    public function clear(){
        $val = $_GET['val'];
        echo target("File")->deleteData($val);
    }

}

